package a07_二叉树;

import java.util.*;

/**
 * <p>
 * a05_二叉树层序遍历复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/20
 */
public class a05_层序遍历01_二叉树的层序遍历复习4 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resultList = new ArrayList<>();
        if (root == null) {
            return resultList;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> result = new ArrayList<>();
            while (size > 0) {
                TreeNode poll = queue.poll();
                result.add(poll.val);
                if(poll.left != null){
                    queue.add(poll.left);
                }
                if(poll.right != null){
                    queue.add(poll.right);
                }
                size--;
            }
            resultList.add(result);
        }
        return resultList;
    }
}
